package linkedlist;

/**
 * @author huangxin
 * @date 2019/6/21-18:43
 * 单向环形列表
 */
public class CircleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode hero4 = new HeroNode(4, "林冲", "豹子头");
        HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
        HeroNode hero2  = new HeroNode(2, "卢俊义", "玉麒麟");

        CircleLinkedList circleLinkedList = new CircleLinkedList(hero1);
        circleLinkedList.add(hero2);
        circleLinkedList.add(hero3);
        circleLinkedList.add(hero4);
        circleLinkedList.list();
    }
}

class CircleLinkedList {
    private HeroNode first;
    private HeroNode current;

    public CircleLinkedList(HeroNode heroNode) {
        this.first = heroNode;
        first.next = heroNode;
        current = heroNode;
    }

    public void add(HeroNode heroNode) {
        current.next = heroNode;
        heroNode.next=first;
        current = heroNode;
    }

    public void list(){
        HeroNode temp = first;
        while (true){
            System.out.println(temp);
            //如果下个节点为第一个节点,则表示循环结束
            if (temp.next == first){
                break;
            }
            temp = temp.next;
        }
    }
}